<template>
	<view class="header">
		<view class="row">
			<view class="refresh" @click.stop="refresh">
				<image :src="$IMG_URL('/static/test/refresh.png')" mode="aspectFill" class="icon"></image>
				<text class="size">刷新</text>
			</view>
			<toggleButton @toggleSwitch="changeIndex" @changeOpen="changeOpen" :isActive="isActiveIndex"
				:computedDay="day" :open="openShow"></toggleButton>

		</view>
		<u-picker mode="time" v-model="openShow" :params="params" :customTimeTag="true" :title="pickerTitle"
			:popupheight="'786rpx'" :borderRadius="16" @confirm="dateConfirm"></u-picker>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import toggleButton from './toggle-button2.vue'
	export default {
		components: {
			toggleButton
		},
		props: {
			shopName: {
				type: String,
				default: '',
				required: true
			},
			firstchange: {
				type: Boolean,
				default: false,
				required: true
			},
			isLoading: {
				type: Boolean,
				default: false,
				required: true
			}
		},
		data() {
			return {
				isActiveIndex: null,
				day: '',
				openShow: false,
				// picker
				pickerTitle: '',
				params: {
					year: false,
					month: false,
					day: false,
					hour: false,
					minute: false,
					second: false
				},
			}
		},
		created() {
			
			this.day = this.formatDate(this.isActiveIndex)
		},
		mounted() {
			if(this.firstchange){
				this.changeIndex(0)
			}
		},
		methods: {
			refresh() {
				if (this.isLoading) {
					uni.showToast({
						title:'请求未完成，请勿重复',
						icon:'none'
					})
					return;
				}
				this.$emit('refresh')
			},
			// toggle() {
			// 	this.$emit('toggle')
			// },
			changeIndex(e) {
				if (this.isLoading) {
					uni.showToast({
						title:'请求未完成，请勿重复',
						icon:'none'
					})
					return;
				}
				console.log('e', e);
				if(this.isActiveIndex != e){
					this.isActiveIndex = e
					this.day = this.formatDate(e)
					this.$emit('changeIndex', e)
				}else{
					this.isActiveIndex = null
					this.day = ''
					this.$emit('changeIndex', null)
				}
				
			},
			formatDate(index) {
				const today = new Date();
				const year = today.getFullYear();
				const month = String(today.getMonth() + 1).padStart(2, '0');
				const day = String(today.getDate()).padStart(2, '0');
				return index === 0 ? `${year}-${month}-${day}` : index === 1 ? `${year}-${month}` : `${year}`
			},
			changeOpen() {
				if (this.isActiveIndex != null) {
					this.openShow = !this.openShow
					if (this.openShow) {
						this.pickerTitle = this.isActiveIndex == 0 ? '选择日期' : this.isActiveIndex == 1 ? '选择月份' : '选择年份'
						let params = {
							year: false,
							month: false,
							day: false,
							hour: false,
							minute: false,
							second: false
						};

						// 根据isActiveIndex设置params的值
						// 简写条件设置
						params.year = this.isActiveIndex < 3;
						params.month = this.isActiveIndex < 2;
						params.day = this.isActiveIndex < 1;
						this.params = params
					}
				} else {
					this.$refs.uToast.show({
						title: '请先选择时段',
						type: 'success',
						icon:false
					})
				}

			},
			dateConfirm(e) {
				if (this.isLoading) {
					uni.showToast({
						title:'请求未完成，请勿重复',
						icon:'none'
					})
					return;
				}
				console.log('e', e, this.$PICKERDATA(e));
				this.day = this.$PICKERDATA(e)
				this.$emit('dateConfirm', e, this.$PICKERDATA(e))
			}
		}
	}
</script>

<style lang="scss" scoped>
	.header_bottom {
		width: 100%;
		background: #ffffff;
	}

	.header {
		width: 702rpx;
		background: #ffffff;

		.shop {
			width: 100%;
			display: flex;
			flex-direction: row;
			align-items: center;
			margin-bottom: 22rpx;

			.Boutique {
				width: 114rpx;
				height: 36rpx;
				margin-right: 12rpx;
			}

			.title {
				flex: 1;
				font-size: 28rpx;
				font-family: PingFang SC, PingFang SC-Bold;
				font-weight: 700;
				text-align: left;
				color: #333333;
			}

			.right {
				display: flex;
				flex-direction: row;
				align-items: center;

				.size {
					font-size: 28rpx;
					font-family: PingFang SC, PingFang SC-Regular;
					font-weight: 400;
					color: #666666;
					margin-right: 8rpx;
				}

				.icon {
					width: 10rpx;
					height: 16rpx;
				}
			}
		}

		.row {
			width: 100%;
			display: flex;
			flex-direction: row;
			align-items: center;
		}

		.refresh {
			width: 122rpx;
			height: 72rpx;
			background: #f6f6f6;
			border-radius: 12rpx;
			margin-right: 12rpx;
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: center;

			.icon {
				width: 22rpx;
				height: 22rpx;
				margin-right: 11rpx;
			}

			.size {
				font-size: 28rpx;
				font-family: PingFang SC, PingFang SC-Medium;
				font-weight: 500;
				text-align: left;
				color: #333333;
			}
		}
	}
</style>